/*
 * Copyright (C) 2011 JiangHongTiao
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package sk.lieskove.jianghongtiao.paris.core;

import org.apache.commons.io.FileUtils;
import org.apache.log4j.Logger;
import sk.lieskove.jianghongtiao.common.utils.PropertiesUtils;
import sk.lieskove.jianghongtiao.commonpersist.Persist;
import sk.lieskove.jianghongtiao.websearch.persistence.ClassificationStorage;
import sk.lieskove.jianghongtiao.websearch.enums.DocumentType;
import sk.lieskove.jianghongtiao.websearch.FullWebSearch;
import sk.lieskove.jianghongtiao.websearch.WebSearch;
import sk.lieskove.jianghongtiao.websearch.service.SearchQuery;
import sk.lieskove.jianghongtiao.websearch.service.SearchQueryImpl;
import sk.lieskove.jianghongtiao.websearch.persistence.WebSearchResponse;

import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;

/**
 * Run on web server with PHP code
 * 
 * @author xjuraj e-mail: jjurco.sk_gmail.com
 */
public class WebProcessing implements Serializable {
    
    private transient Logger log = Logger.getLogger(WebProcessing.class);
    private transient PropertiesUtils pu = new PropertiesUtils(WebProcessing.class);
    
    
    public static void main( String[] args ) throws IOException {
        File file = new File("lock");
        if(file.exists()){
            System.err.println("Another instance is running! Exit...");
            return;
        }
        file.createNewFile();
        file.deleteOnExit();
        
        File searchFile = new File("searchString");
        if(!searchFile.exists()){
            System.err.println("Cannot read serch file string");
            return;
        }
        String readFileToString = FileUtils.readFileToString(searchFile, "UTF-8");
        searchFile.deleteOnExit();
        
        WebSearch es = new FullWebSearch();
        SearchQuery query = new SearchQueryImpl();
        query.addWord(readFileToString);
//        query.addWord("Katarína Janigáčová");
        ArrayList<WebSearchResponse> searchAll = 
                (ArrayList<WebSearchResponse>) es.searchAll(query);
        Persist p = Persist.getSingleton();
        for (int i = 0; i < searchAll.size(); i++) {
            WebSearchResponse resp = searchAll.get(i);
            ClassificationStorage cs = new ClassificationStorage();
            cs.setDocumentType(DocumentType.UNKNOWN);
            p.remoteObjects(resp);
            cs.setWebSearchResponse(resp);
            p.remoteObjects(cs);
        }
    }
}
